#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
@File    :   img_utlis.py
@Time    :   2024/12/13 23:45:01
@Author  :   xiangyu Chen
@Version :   1.0
@Contact :   yu112yu@sina.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA
@Desc    :   None
'''

import ddddocr
from PIL import Image

from utils.my_error import MyError

# def all_img(driver,output,)


# JS={
#     'rollBottom':"window.scroll({top:document.body.clientHeight,left:0,behavior:'auto'})",
#     'rollX':"window.scroll({top:%d,leet:0,behavior:'auto'})",
# }

# def join_img(p1, p2, size=0, output='result.png'):
#     """图片拼接

#     Parameters
#     ----------
#     p1 : _type_
#         图片1
#     p2 : _type_
#         图片2
#     size : int, optional
#         重叠距离, by default 0
#     output : str, optional
#         输出的图片, by default 'result.pny'
#     """
#     img1, img2 = Image.open(p1), Image.open(p2)
#     s1, s2 = img1.size, img2.size
#     joint = Image.new('RGB', (s1[0], s1[1] + s2[1] - size))
#     # 设置两张图片要旋转的初始位置
#     loc1, loc2 = (0, 0), (0, s1[1] - size)

#     joint.paste(img1, loc1)
#     joint.paste(img2, loc2)
#     img1.close()
#     img2.close()
#     joint.save(output)

# join_img('img\20241213-225847_all.png','img\20241213-230614_.png','img\20241215.png')



def ocr_verify_code(img, ex_len=4):
    '''
    识别验证码
    传入图片路径或二进制均可
    ex_len: 预期长度
    '''
    ocr = ddddocr.DdddOcr()
    # img_path = './img/ddddv.png'
    if isinstance(img, (bytes, bytearray)) != True:
        with open(img, 'rb') as f:
            img = f.read()
    # print('111111111111111;   ', img.__class__)
    res = ocr.classification(img)
    if (len(res) == ex_len):
        return res
    else:
        raise MyError(f'verify len error: ex_len is {ex_len}--------ocr_len is {len(res)}')
        # raise EXCEPTION(f'verify len error: ex_len is {ex_len}--------ocr_len is {len(res)}')
